package controllers;

import java.util.List;

import models.User;

import org.apache.commons.lang.StringUtils;

import play.Play;
import play.db.jpa.JPABase;
import play.mvc.Before;

public class UserController extends BaseController {
    
    private final static String EMPTY_PASSWORD_ERROR = "密码不能为空";
    
    private final static String PASSWORD_DIFF_ERROR = "老密码不正确";
    
    private final static String UPDATE_PASSWORD_SUCCESS = "";
    
    
    public static void updatePassword(String oldPassword, String newPassword) {
        if (StringUtils.isBlank(oldPassword) || StringUtils.isBlank(newPassword)) {
            renderText(EMPTY_PASSWORD_ERROR);
        }
        
        String currentUserid = getCurrentUserid();
        User current = User.findById(Long.valueOf(currentUserid));
        if (current == null) {
            notFound();
        }
        if (!StringUtils.equals(oldPassword, current.password)) {
            renderText(PASSWORD_DIFF_ERROR);
        }
        
        current.updatePassword(current, newPassword);
        renderText(UPDATE_PASSWORD_SUCCESS);
    }
}
